'''
Created on May 8, 2011

@author: Armon
'''
import nltk_contrib.readability.syllables_en as syllables
import en
import FeatureSuggestions
import Suggestion
import string

class AvgSyllablesPerWord():
    '''
    classdocs
    '''

    def __init__(self, featureHandler, feature, noOfDocumentSyllables):
        '''
        Constructor
        '''
        
        self.featureSuggestions = []
        self.noOfDocumentSyllables = noOfDocumentSyllables
        usedSynonyms = []
        
        '''For each section in the paragraphs of the current document:
            For each sentence in that section:'''
        for section in range(len(featureHandler.documentSections)):
            documentSentences = featureHandler.documentSections[section].getSentences()
         
            for sentence in range(len(documentSentences)):
                wordsInSentence = documentSentences[sentence].getWords()
                tempSentence = ''
                
                '''Get each word in the sentence to get a string of words'''
                for word in range(len(wordsInSentence)):
                    '''See of the word exists in the dictionary'''
                    tempSentence+= ' ' + wordsInSentence[word]

                
                taggedSentence = en.sentence.tag(tempSentence)
             
                for i in range(len(taggedSentence)):
                    
                    tempString = str(taggedSentence[i][0])
                    tempString = self.cleanString(tempString);

                    tempCheck = taggedSentence[i][1]
                    '''Get the number of syllables in the word'''
                    self.noOfSyllables = syllables.count(tempString)
                    '''Get a list of synonyms for that word. See getSynonyms function comments
                    for details on how it works'''
                    wordSynonyms = self.getSynonyms(tempCheck, tempString, taggedSentence)
                    
                    '''If a list of synonyms is returned and not a blank list'''
                    if len(wordSynonyms) > 0:
                        
                        '''Using the list of synonyms, find a synonym for that word with more/less words. See the comments for
                        findSynonym for the details on how it works'''
                        synonym = self.findSynonym(wordSynonyms, tempString)
                        if str(synonym) != 'None':
                            if tempCheck == "NNS":
                                '''Make the synonym plural again since in the original sentence it was plural'''
                                synonym = en.noun.plural(str(synonym))
                            '''Append the synonym to the list of synonyms'''
                            usedSynonyms.append(synonym)
                                
                            featureSuggestion = FeatureSuggestions.FeatureSuggestions(feature)
                            if usedSynonyms.count(synonym) == 1:
                                '''Create a new suggestion with the synonym to suggest with more/less syllables as a change'''
                                self.avgSyllablesPerWordSuggestions = Suggestion.Suggestion('Syllable change', featureHandler.document, tempString, synonym, '#00FF00')
                                featureSuggestion.addSuggestions(self.avgSyllablesPerWordSuggestions)
                                self.featureSuggestions.append(featureSuggestion)
        
    def getFeatureSuggestions(self):
        return self.featureSuggestions
    
    def cleanString(self, tempString):
        tempString = tempString.replace(',', '')
        tempString = tempString.replace('"', '')
        return tempString
    
    def getSynonyms(self, tempCheck, tempString, taggedSentence):
        
        synonyms = []
        if tempString != "":
            if((tempCheck == "NN") or (tempCheck == "NNS")):
                if tempCheck == "NNS":
                    synonyms = en.noun.hypernyms(en.noun.singular(tempString))
                else:
                    synonyms = en.noun.hypernyms(tempString)
         
            elif ((tempCheck == "JJ") or (tempCheck == "JJR") or (tempCheck == "JJS")):
                synonyms = en.adjective.hypernyms(tempString)
         
            elif ((tempCheck == "RB") or (tempCheck == "RBR") or (tempCheck == "RBS")):
                synonyms = en.adverb.hypernyms(tempString)
                
        return synonyms
    
    def findSynonym(self, wordSynonyms, originalWord):
        '''For each synonym in the list returned'''
        for i in range(len(wordSynonyms)):
            tempList = wordSynonyms[i]
                            
            for j in range(len(tempList)):
                if tempList[j] != originalWord:
                    synonymSyllables = syllables.count(str(tempList[j]))
                    '''If the number of syllables matches the criteria for use in the document, use that synonym'''
                    if (((synonymSyllables < self.noOfSyllables) and (self.noOfSyllables > self.noOfDocumentSyllables)) or ((synonymSyllables > self.noOfSyllables) and (self.noOfSyllables < self.noOfDocumentSyllables))):
                        synonymToUse = str(tempList[j])
                        return synonymToUse
                
if __name__ == '__main__': 
    driver = AvgSyllablesPerWord()